package com.mdd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.R;
import com.mdd.dto.VoucherOrderDto;
import com.mdd.pojo.request.VoucherOrderAddRequest;
import com.mdd.pojo.request.VoucherOrderPageRequest;
import com.mdd.service.VoucherOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/vorder")
public class VoucherOrderController {

    @Autowired
    private VoucherOrderService voucherOrderService;

    /**
     * 生成普通优惠券订单
     * @param voucherOrderAddRequest
     * @return
     */
    @PostMapping("/voucher/add")
    private R<Long> addOrder(@RequestBody VoucherOrderAddRequest voucherOrderAddRequest){
        //检验
        if (voucherOrderAddRequest == null) {
            return R.error("生成订单失败");
        }
        return voucherOrderService.createVoucherOrder(voucherOrderAddRequest);
    }

    /**
     * 生成秒杀优惠券订单
     * @param voucherOrderAddRequest
     * @return
     */
    @PostMapping("/seckillVoucher/add")
    private R<Long> addSeckillOrder(@RequestBody VoucherOrderAddRequest voucherOrderAddRequest){
        //校验
        if (voucherOrderAddRequest == null) {
            return R.error("生成订单失败");
        }
        //生成订单
        return voucherOrderService.createSeckillOrder(voucherOrderAddRequest);
    }

    /**
     * 分页查询(条件)
     * @param voucherOrderPageRequest
     * @return
     */
    @GetMapping("/page")
    public R<Page<VoucherOrderDto>> showVoucherList(VoucherOrderPageRequest voucherOrderPageRequest){
        //校验
        if (voucherOrderPageRequest == null) {
            return R.error("展示失败");
        }
        return voucherOrderService.voucherOrderPage(voucherOrderPageRequest);

    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public R<String> deleteOrder(Long id){
        if (!voucherOrderService.removeById(id)) {
            return R.error("删除失败");
        }
        return R.success("删除成功!");
    }
}
